package com.apex.restful.service.impl;

import com.apex.restful.domain.SequenceRepository;
import com.apex.restful.entity.TSequence;
import com.apex.restful.service.TSequenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * Created by wangjian on 2017/3/28.
 */
@Service
public class TSequenceServiceImpl implements TSequenceService {

    @Autowired
    SequenceRepository sequenceRepository;

    public TSequence findTSequenceByName(String name) {
        if(!StringUtils.isEmpty(name)){
            TSequence sequence= sequenceRepository.findOne(name);
            return sequence;
        }
        return null;
    }

    @Transactional
    public int updateTSequenceByName(TSequence sequence) {
        if(null!=sequence){
            sequenceRepository.updateTSequenceByName(sequence.getId(),sequence.getName());
        }
        return 0;
    }

    @Transactional
    public void saveTSequence(TSequence sequence) {
        sequenceRepository.save(sequence);
    }

    public  long returnId(String name){
        if(!StringUtils.isEmpty(name)){
            TSequence sequence =sequenceRepository.findOne(name);
            if(null!=sequence){
                long id=sequence.getId()+1;
                sequence.setId(id);
                sequenceRepository.updateTSequenceByName(id,name);
                return id;
            }else {
                TSequence sequence1=new TSequence();
                sequence1.setId(Long.parseLong("1"));
                sequence1.setName(name);
                sequenceRepository.save(sequence1);
            }
        }
        return 1;
    }
}
